package com.liweitong.minhang.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liweitong.minhang.entity.Flight;
import com.liweitong.minhang.entity.FlightQueryVo;
import com.liweitong.minhang.mapper.FlightMapper;
import com.liweitong.minhang.service.FlightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @Author Weton Li
 * @Date 2021/5/21 19:17
 */
@Controller
public class FlightController {

    @Autowired
    FlightService flightService;


    /**
     * 前往登录页面
     *
     * @return
     */
    @GetMapping("/login")
    public String toLoginPage() {
        return "login";
    }

//    /**
//     * 表单提交的action方法 请求/login.html
//     *
//     * @return
//     */
//    @PostMapping("/login.html")
//    public String login() {
//        return "redirect:/list.html";
//    }
    // 人脸识别登录

    /**
     * 重复刷新这次轻松的请求
     *
     * @return
     */
//    @GetMapping("/list.html")
//    public String toListPage() {
//        return "forward:/flightList";
//    }


//    @GetMapping("/helloThymeleaf")
//    public String helloThymeleaf(Model model){
//        model.addAttribute("msg","你好");
//        return "list";
//    }


    /**
     * 显示航班列表
     *
     * @param model
     * @return
     */
//        @ResponseBody
//    @GetMapping("/flightList")
//    public String getFlightList(Model model) {
//        List<Flight> list = flightService.list();
//        model.addAttribute("flights", list);
//        return "list";
//    }

    @RequestMapping("/queryWithConditional")
    public String queryWithConditional(String fno,String fdate,Model model) {

        QueryWrapper<Flight> wrapper = new QueryWrapper<>();
//        String fno = flightQueryVo.getFno();
//        Date fdate = flightQueryVo.getFdate();
        if (!StringUtils.isEmpty(fno)) {
            wrapper.eq("fno", fno);
        }
        if (!StringUtils.isEmpty(fdate)) {
            wrapper.eq("fdate", fdate);
        }
        List<Flight> list = flightService.list(wrapper);
        model.addAttribute("flights",list);
        return "list";
    }

    //    @ResponseBody
    @RequestMapping("/addFlight")
    public String addFlight(Flight flight) {
        flightService.save(flight);
        return "redirect:/flightList";
    }


    //    @ResponseBody
    @RequestMapping("/deleteFlight/{fno}")
    public String deleteFlight(@PathVariable("fno") String fno) {
        flightService.removeById(fno);
        return "redirect:/flightList";
    }

    /**
     * 只负责跳转到民航修改页面
     *
     * @param fno
     * @return
     */
    @RequestMapping("/toUpdatePage/{fno}")
    public String toUpdatePage(@PathVariable("fno") String fno, Model model) {
        Flight flight = flightService.getById(fno);
        model.addAttribute("flight", flight);
        return "update";
    }

    /**
     * 修改页面的表单点击更新后
     *
     * @param flight
     * @return
     */
    @RequestMapping("/updateFlight")
    public String updateFlight(Flight flight) {
        flightService.updateById(flight);
        return "redirect:/flightList";
    }

}
